package com.tianya.forum.controller;


import com.tianya.forum.common.AppResult;
import com.tianya.forum.common.ResultCode;
import com.tianya.forum.config.AppConfig;
import com.tianya.forum.exception.ApplicationException;
import com.tianya.forum.model.Article;
import com.tianya.forum.model.ArticleReply;
import com.tianya.forum.model.User;
import com.tianya.forum.services.impl.ArticleReplyServiceImpl;
import com.tianya.forum.services.impl.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private ArticleReplyServiceImpl articleReplyService;
    @Resource
    private ArticleService articleService;


    /**
     * 回复帖子
     * @param articleId 帖子ID
     * @param content 回复正文
     * @return
     */
    @ApiOperation(value = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖子ID") @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content){

        // 获取⽤⼾信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断是否被禁⾔
        if (user.getState() != 0) {
            // ⽇志
            log.warn(ResultCode.FAILED_USER_BANNED.toString() + ", userId = "
                    + user.getId());
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取帖⼦信息
        Article article = articleService.selectById(articleId);
        // 校验帖⼦信息
        if (article == null) {
            log.warn("修改的帖⼦不存在. articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn("帖⼦状态异常. articleId = " + articleId + ", state = " +
                    article.getState() + ", delete state = " + article.getDeleteState());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 构造帖子回复数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        // 调⽤业务层
        articleReplyService.creat(articleReply);
        return AppResult.success();
    }

    @GetMapping("/getReplies")
    @ApiOperation(value = "帖子回复")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @ApiParam(value = "帖子ID")@RequestParam(value = "articleId")@NonNull Long articleId){

        //查询帖子是否存在
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            //判断帖子状态是否异常
            log.warn(ResultCode.FAILED_ARTICLE_FORBIDDEN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_FORBIDDEN));
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
